﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DCT.CodeBox.LightLog.Codes
{
    /*
     配置格式
     <root>
        <!-- 日志级别，如果是空字符串或all，就所有级别都记录；如果需要过滤，则Debug,Info,Warning,Error,Fatal，用逗号隔开 -->
        <level>all</level>
     </root>
     */

    /// <summary>
    /// 控制台志日
    /// </summary>
    public class ConsoleLog : StringLogSkeleton
    {
        private static Dictionary<LogLevel, ConsoleColor> LogLevelConsoleColorMapper = new Dictionary<LogLevel, ConsoleColor>();

        static ConsoleLog()
        {
            LogLevelConsoleColorMapper[LogLevel.Debug] = ConsoleColor.White;
            LogLevelConsoleColorMapper[LogLevel.Info] = ConsoleColor.Blue;
            LogLevelConsoleColorMapper[LogLevel.Warning] = ConsoleColor.Yellow;
            LogLevelConsoleColorMapper[LogLevel.Error] = ConsoleColor.DarkRed;
            LogLevelConsoleColorMapper[LogLevel.Fatal] = ConsoleColor.Red;
        }

        protected override void WriteLine(LogLevel level, string str)
        {
            ConsoleColor resumeColor = Console.ForegroundColor;

            ConsoleColor logColor;
            if (!LogLevelConsoleColorMapper.TryGetValue(level, out logColor))
            {
                logColor = resumeColor;
            }
            Console.ForegroundColor = logColor;
            Console.WriteLine(str);

            Console.ForegroundColor = resumeColor;
        }
    }
}
